precision mediump float;

varying vec3 v_VertPosition;
varying vec2 v_VertTexCoord;
varying vec3 v_VertNorm;
varying vec3 v_VertBiNorm;
varying vec3 v_VertTangent;

uniform vec3 u_CameraPosition;
uniform vec3 u_LightPosition;
uniform sampler2D u_NormMapTexture;
uniform sampler2D u_ObjTexture;
uniform vec4 u_Step;

void main()
{
	vec3 color0 = texture2D(u_ObjTexture, v_VertTexCoord).rgb;
	vec3 color1 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x+u_Step.x, v_VertTexCoord)).rgb;
	vec3 color2 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x-u_Step.x, v_VertTexCoord)).rgb;
	vec3 color3 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x, v_VertTexCoord.y+u_Step.y)).rgb;
	vec3 color4 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x, v_VertTexCoord.y-u_Step.y)).rgb;
	vec3 color5 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x+u_Step.z, v_VertTexCoord.y+u_Step.w)).rgb;
	vec3 color6 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x-u_Step.z, v_VertTexCoord.y+u_Step.w)).rgb;
	vec3 color7 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x+u_Step.z, v_VertTexCoord.y-u_Step.w)).rgb;
	vec3 color8 = texture2D(u_ObjTexture, vec2(v_VertTexCoord.x-u_Step.z, v_VertTexCoord.y-u_Step.w)).rgb;
	//vec4 cObjComponent = vec4((color0*2.0 + color1 + color2 + color3 + color4 + color5 + color6 + color7 + color8)*0.1, 1.0);
	vec4 cObjComponent = vec4(color0, 1.0);
	float fWeight = 0.2;
	vec3 vNormalMap = (texture2D(u_NormMapTexture, v_VertTexCoord).xyz);
	mat3 mTBN = mat3(normalize(v_VertTangent), normalize(v_VertBiNorm), normalize(v_VertNorm));
	vec3 vNorm = normalize(mTBN * (2.0 * vNormalMap- 1.0));
	vec3 vLightDirection = normalize(v_VertPosition - u_LightPosition);
	vec4 cDiffuseComponent = max(dot(-vLightDirection, vNorm), 0.0) * vec4(1.0, 1.0, 1.0, 1.0);
	vec4 cAmbientComponent = vec4(1.0, 1.0, 1.0, 1.0);
	vec3 vReflect = normalize(reflect(vLightDirection, vNorm));
	vec3 vToEye = normalize(u_CameraPosition - v_VertPosition);
	vec4 cSpecularComponent = pow(max(dot(vReflect, vToEye), 0.0), 20.0) * vec4(1.0, 1.0, 1.0, 1.0);
	gl_FragColor = vec4(((cAmbientComponent * fWeight + cDiffuseComponent * (1.0 - fWeight)) * cObjComponent).xyz, cObjComponent.w);
	 
}